package com.dexilaonian.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.activity.ConsultationActivity;
import com.dexilaonian.activity.HealthRecordActivity;
import com.dexilaonian.activity.LoginActivity;
import com.dexilaonian.activity.MangerAddressActivity;
import com.dexilaonian.activity.MyStoreOrderAcitivity;
import com.dexilaonian.activity.NewsActivity;
import com.dexilaonian.activity.OrderActivity;
import com.dexilaonian.activity.Piety_card;
import com.dexilaonian.activity.ServiceActivity;
import com.dexilaonian.activity.SetActivity;
import com.dexilaonian.activity.UserActivity;
import com.dexilaonian.ui.BaseFragment;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.ImageLoader;
import com.dexilaonian.utils.Logutil;
import com.dexilaonian.utils.Okhttp;
import com.dexilaonian.utils.PreferencesUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class MeFragment extends BaseFragment {

    @Bind(R.id.me_iv_user)
    ImageView meIvUser;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.me_phone)
    TextView mePhone;
    @Bind(R.id.me_background)
    RelativeLayout meBackground;
    @Bind(R.id.shouhuodizhi)
    TextView shouhuodizhi;
    @Bind(R.id.rl_me_order)
    TextView rlMeOrder;
    @Bind(R.id.shangpdingdan)
    TextView shangpdingdan;
    @Bind(R.id.me_wallet)
    ImageView meWallet;
    @Bind(R.id.rl_xiaoxinka)
    RelativeLayout rlXiaoxinka;
    @Bind(R.id.me_news)
    ImageView meNews;
    @Bind(R.id.rl_me_news)
    RelativeLayout rlMeNews;
    @Bind(R.id.me_health)
    ImageView meHealth;
    @Bind(R.id.rl_health_record)
    RelativeLayout rlHealthRecord;
    @Bind(R.id.me_set)
    ImageView meSet;
    @Bind(R.id.rl_me_set)
    RelativeLayout rlMeSet;
    private String me_url;
    private String key;
    private String phone;
    private Intent intent;
    private int status;
    private FragmentActivity mContext;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (mContext == null) {
            mContext = getActivity();
        }

    }
    @Override
    public void onResume() {
        super.onResume();
        Logutil.e("TTTonResume");
        key = PreferencesUtil.getString(mContext, Constant.KEY);
        me_url = Constant.base_url + "/user/getMyInfo.do";
        new Thread(new Runnable() {
            @Override
            public void run() {
                okMefragment();
            }
        }).start();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    protected void initView(View layoutView) {

    }

    @Override
    public int getLayoutID() {
        return R.layout.fragment_me;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Logutil.e("TTTonActivityCreated");
    }

    private void okMefragment() {
        Map<String, String> map = new HashMap<>();
        Okhttp.postString(true, me_url, map, new Okhttp.CallBac() {

            @Override
            public void onError(Call call, Exception e, String state, int id) {
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String data = jsonObject.optString("data");
                    status = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 0) {
                        if (data != null) {
                            JSONObject json = new JSONObject(data);
                            phone = json.optString("phone");
                            String userName = json.optString("userName");
                            if (!TextUtils.isEmpty(userName)) {
                                Log.e("TTTNAME", userName);
                                name.setText(userName);
                            }
                            if (!TextUtils.isEmpty(phone)) {
                                mePhone.setText(phone);
                            }
                            String face = json.optString("face");
                            if (face != null || !TextUtils.isEmpty(face)) {
                                ImageLoader.displayCircle1(mContext, face, meIvUser);
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNoNetwork(String state) {

            }
        });
    }

    private void initService() {
        showProgressBar(true);
        Map<String, String> map = new HashMap<>();
        String vip_url = Constant.base_url + "/vipCard/list.do";
        ;
        Okhttp.postString(true, vip_url, map, new Okhttp.CallBac() {
            @Override
            public void onError(Call call, Exception e, String state, int id) {
                showProgressBar(false);
                showToast(state);
            }

            @Override
            public void onResponse(String response, int id) {
                showProgressBar(false);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int status = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 16 || status == 34) {
                        startActivity(new Intent(mContext,LoginActivity.class));
                        return;
                    }
                    if (status == 35) {
                        intent = new Intent(mContext, MangerAddressActivity.class);
                        intent.putExtra("CONFIRM", "");
                        startActivity(intent);
                        return;
                    }
                    startActivity(new Intent(mContext, Piety_card.class));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNoNetwork(String state) {
                showProgressBar(false);
                showToast(state);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick({R.id.me_background, R.id.shouhuodizhi, R.id.rl_me_order, R.id.shangpdingdan, R.id.rl_xiaoxinka, R.id.rl_me_news, R.id.rl_health_record, R.id.rl_me_set})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.me_background:
                startActivity(new Intent(mContext, UserActivity.class));
                break;
            case R.id.shouhuodizhi:
                intent = new Intent(mContext, MangerAddressActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_me_order:
                startActivity(new Intent(mContext, OrderActivity.class));
                break;
            case R.id.shangpdingdan:
                startActivity(new Intent(mContext, MyStoreOrderAcitivity.class));
                break;
            case R.id.rl_xiaoxinka:
                initService();
                break;
            case R.id.rl_me_news:
                startActivity(new Intent(mContext, NewsActivity.class));
                break;
            case R.id.rl_health_record:
                startActivity(new Intent(mContext, ConsultationActivity.class));
                break;
            case R.id.rl_me_set:
                intent = new Intent(mContext, SetActivity.class);
                if (!TextUtils.isEmpty(phone)) {
                    intent.putExtra("Phone", phone);
                }
                startActivity(intent);
                break;
        }
    }
}
